/*
 *	Defines IO operations for the cluster data
 * 
 *	To Do:
 *	1)	Allow deep search of files, i.e. go into 
 *	further directories and search for additional 
 *	data files to parse
 *	2)	Create file for logging errors, e.g. errors
 *	opening files, unexpected return, etc.
 *	
 *	Notes:
 *	Maybe have the parsing method(s) be called if a
 *	successful open was done?
 * 
 *
 * Changelog:
 * (How do you feel about having a changelog in all files?)
 * 
 * Update March 13th 
 * I changed the methods to be non-static so that we can 
 * make instances of the dataIO class.
 */
import java.io.*;
import java.util.Hashtable;

public class dataIO{

	public int listDirectory()
	{
		
		parseToDB db = new parseToDB("cluster_data");
		db.ensureIndexes();
		//int fileExtensionIndex = -1;
		Hashtable<String, Integer> methods = new Hashtable<String, Integer>();
		methods.put("job_events",1);
		methods.put("machine_attributes",2);
		methods.put("machine_events",3);
		methods.put("task_constraints",4);
		methods.put("task_events",5);
		methods.put("task_usage",6);

		//File directory = new File("C:\\Users\\Sara\\Documents\\CSCI3907.83\\google-traces-1-2");
		File directory = new File("../ClusterData/");
		
		FileFilter dirFilter = new FileFilter() {	// Returns directories
			public boolean accept(File file) {
				return file.isDirectory();
			}
		};

		File subDirectories[] = directory.listFiles(dirFilter);	// Returns the sub dirs of directory
		for(File subDir: subDirectories)
		{			
			File subFiles[] = subDir.listFiles();
			if(!methods.containsKey(subDir.getName()))
				continue;
			int method = (methods.get(subDir.getName())).intValue();
	
			
			for(File F : subFiles)
			{
				System.out.println("  " + F.getName());
				BufferedReader input;
				try {
					input = new BufferedReader(new FileReader(F));
					switch (method)
					{
						case 1:		db.jobEvents(input);
									break;
						case 2:		db.machineattr(input);
									break;
						case 3:		db.machineEvents(input);
									break;
						case 4:		db.taskConstraints(input);
									break;
						case 5:		db.taskEvents(input);
									break;
						case 6:		db.taskUsage(input);
									break;
						default:	System.out.println("failed choosing method");
					}
				} catch (FileNotFoundException e) {
					System.out.println("Error opening file: " + F);
					continue;
				}

			}
		}

		return 0;
	}
}